//Constructs Summary States for long run rates of return in equities, bonds and treasures. Tables A5 and A6

**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
//Useful Programs



//Program that calculates various summary statistics
capture program drop sumstatsprog2 
program define sumstatsprog2,rclass
tempname tempstat tempstatsTransformed

qui tabstat `1' `2' `3' ,statistics(n mean p50 sd p90 p10 ) save
quiet tabstatmat `tempstat'
quiet matrix `tempstatsTransformed' = J(3,5,0)


 forvalues j = 1/4 {	
matrix `tempstatsTransformed'[1,`j']= `tempstat'[`j',1] 
matrix `tempstatsTransformed'[2,`j']= `tempstat'[`j',2] 
matrix `tempstatsTransformed'[3,`j']= `tempstat'[`j',3]
}

matrix `tempstatsTransformed'[1,5]= `tempstat'[5,1] -`tempstat'[6,1] 
matrix `tempstatsTransformed'[2,5]= `tempstat'[5,2] -`tempstat'[6,2] 
matrix `tempstatsTransformed'[3,5]= `tempstat'[5,3] -`tempstat'[6,3] 


//matrix rownames `tempstatsTransformed' = erir brir trir
//matrix colnames `tempstatsTransformed' = N Mean  Median Std p90-p10 

//return mat x = `tempstatsTransformed'


tempfile tempsave
save `tempsave'
 
drop _all
svmat double `tempstatsTransformed'

format  %9.3f `tempstatsTransformed'*
format  %9.0f `tempstatsTransformed'1


rename `tempstatsTransformed'1 N
rename `tempstatsTransformed'2 Mean
rename `tempstatsTransformed'3 Median
rename `tempstatsTransformed'4 Std
rename `tempstatsTransformed'5 p90top10

gen names="erir"
replace names="brir" if _n==2 
replace names="trir" if _n==3


outsheet names N Mean Median Std p90top10 using "`4'" , comma replace

drop _all
use `tempsave'



end







**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
**********************************************************************************************
// Draw distribution of annual and generational normalized rates of return in equities, treasuries and bonds, Figure XX
// Create Tables showing summary statistics for annual/generational rates of return in equities, treasuries and bonds
//local base "C:/Users/rls7/Dropbox/Shared_Projects/Long run beta/CodeData_Radek"

use "$base/Outputs/Data/longRunReturnsData.dta", clear

drop if missing(erirA, brirA, trirA)

//drop if missing(erirG, brirG, trirG)
drop if countrycode=="WLD" //Drop World
drop if countrycode=="WXUS" //Drop World excluding US
drop if countrycode=="EUR" //Drop Europe


sort countrycode year

merge countrycode using "$base/InputData/popWDI2000.dta"

drop if missing( sp_pop_totl)



//Calculate normalized returns
foreach j in "A" "G" { //For annual and generational interest rates
foreach i in "e" "b" "t"{ //For equities, bonds and treasuries

gen `i'grir`j'=ln(1+`i'rir`j') //Taking logs doesn't make much difference, could be cleaner
qui summarize `i'grir`j' 
gen norm`i'grir`j'=`i'grir`j'-r(mean) //If I dont log the above, then change to division rather than subtraction.
drop `i'grir`j'

//qui summarize norm`i'grir`j'  ,d
//gen norm`i'grir`j'p90p10=r(p90)-r(p10)
}
}

order erirA brirA trirA erirG brirG trirG normegrirA normbgrirA normtgrirA normegrirG normbgrirG , before(normtgrirG )



foreach j in "A" "G" { //For annual and generational interest rates
sumstatsprog2 erir`j' brir`j' trir`j' "$base/Outputs/Tables/sumstatsrir`j'.csv"
sumstatsprog2 normegrir`j' normbgrir`j' normtgrir`j' "$base/Outputs/Tables/sumstatsnormrir`j'.csv"
}

use "$base/Outputs/Data/longRunReturnsData.dta", clear
drop if missing(erirA, brirA, trirA)
keep if countrycode=="WLD" //Drop World
foreach j in "A" "G" { //For annual and generational interest rates
sumstatsprog2 erir`j' brir`j' trir`j' "$base/Outputs/Tables/sumstatsWrir`j'.csv"
}



